package com.me.adapter;

import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.me.app.R;
import com.me.data.MeEvent;
import com.me.util.AppConstant;
import com.me.util.AppUtil;

public class EventListAdapter extends ArrayAdapter<MeEvent> {
	
	public EventListAdapter(Context context, List<MeEvent> eventList) {
		super(context, R.layout.event_item, eventList);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		EventItemView eventItemView;

		if (convertView == null) {
			// inflate the list view item layout
			LayoutInflater inflater = LayoutInflater.from(getContext());
			convertView = inflater.inflate(R.layout.event_item, parent, false);

			// initialize the event item view
			eventItemView = new EventItemView();
			
			eventItemView.eventTitle = (TextView) convertView
					.findViewById(R.id.event_item_title);
			eventItemView.eventDate = (TextView) convertView
					.findViewById(R.id.event_item_date);
			eventItemView.eventStart = (TextView) convertView
					.findViewById(R.id.event_item_start);
			eventItemView.eventEnd = (TextView) convertView
					.findViewById(R.id.event_item_end);
			eventItemView.eventVenue = (TextView) convertView
					.findViewById(R.id.event_item_venue);

			convertView.setTag(eventItemView);

		} else {
			// recycle the already inflated view
			eventItemView = (EventItemView) convertView.getTag();
		}

		final MeEvent event = (MeEvent) getItem(position);

		// set date
		if (AppUtil.Event.eventType == AppConstant.EVENT_INDEX_PENDING) {
			eventItemView.eventDate.setText(event
					.getFormattedStart("yyyy-MM-dd EEE"));
		} else {
			eventItemView.eventDate.setText(event.getFormattedStart("d EEE"));
		}
		if (position == 0) {
			eventItemView.eventDate.setVisibility(View.VISIBLE);
		} else {
			eventItemView.eventDate.setVisibility(View.GONE);
			MeEvent prevEvent = (MeEvent) getItem(position - 1);
			if (prevEvent.getCalStart().get(Calendar.DAY_OF_MONTH) != event
					.getCalStart().get(Calendar.DAY_OF_MONTH))
				eventItemView.eventDate.setVisibility(View.VISIBLE);
		}

		// set event time
		if (event.isAllDay()) {
			eventItemView.eventStart.setText(AppConstant.EVENT_ITEM_ALL_DAY);
			eventItemView.eventEnd.setVisibility(View.GONE);
		} else {
			eventItemView.eventEnd.setVisibility(View.VISIBLE);
			eventItemView.eventStart.setText(event.getFormattedStart("HH:mm"));
			eventItemView.eventEnd.setText(event.getFormattedEnd("HH:mm"));
		}

		// set event title
		String title = event.getTitle();
		if (title != null && title.length() > 0) {
			eventItemView.eventTitle.setText(title);
		} else {
			eventItemView.eventTitle.setText(AppConstant.EVENT_ITEM_NO_TITLE);
		}

		// set event venue
		String venue = event.getVenue();
		if (venue != null && venue.length() > 0) {
			eventItemView.eventVenue.setVisibility(View.VISIBLE);
			eventItemView.eventVenue.setText(venue);
		} else {
			eventItemView.eventVenue.setVisibility(View.GONE);
		}
		
				
		return convertView;
	}

	public static class EventItemView {
		public TextView eventTitle;
		TextView eventDate;
		TextView eventStart;
		TextView eventEnd;
		TextView eventVenue;
	}

}
